import time  # 导入时间模块，用来显示时间
import shelve as sl  # 导入shelve模块，用来存储数据
import form_say_tools as fst  # 导入项目工具模块

adm_password = "102938"  # 设置管理员(adm)密码,可以修改
password_count = 10  # 设置输入密码次数
yes_list = ["Y", "y", "YES", "Yes", "yes", "是", "1"]
hi = True


def main():
    name = csh()
    choose = function_dir()
    print(choose)
    if choose == "查看留言":
        read_data(name)
    elif choose == "留下留言":
        add_data(name)
    elif choose == "删除留言":
        del_data(name)
    elif choose == "退出程序":
        out()


def csh():
    global password_count, hi
    with sl.open("data") as data:
        if "you_name" in data:
            name = data["you_name"]
            if hi:
                if name == "adm":
                    print("欢迎管理员(adm)回来————")
                else:
                    print(f"hi {name}, 欢迎回来——")
                hi = False
            fst.print_line(10, "——", True)
            return name
        else:
            fst.print_line(10, "——", True)
            print("欢迎来到SAY！,这里应该有给你的留言。", end="")
            fst.print_line(10, "——", True)
            you_name = input("\n请问我该称呼你什么好呢? ")
            if you_name == "adm":
                password = input("请输入管理员密码:")
                while True:
                    if password == adm_password:
                        data["you_name"] = "adm"
                        data.update({"adm": []})
                        return "adm"
                    else:
                        if password_count <= 0:
                            print("密码输入错误过多")
                            password_count = -1
                            exit(0)
                        else:
                            password = input(f"密码错误,请重新输入密码(剩余{password_count}次):")
                            password_count = password_count - 1

            else:
                data["you_name"] = you_name
                return you_name


def function_dir():
    fst.print_line(10, "——", True)
    function_list = ["查看留言", "留下留言", "删除留言", "退出程序"]
    time_str = time.strftime("%Y/%m/%d %H:%M")
    print("\n现在的时间:" + time_str)
    print("目录列表:\n  查看留言(1)\n  留下留言(2）\n  删除留言(3)\n  退出程序(4)")
    # 查看留言、留下留言、删除留言（需要管理员权限）、退出程序
    choose = input("请输入您要执行的操作:")
    if choose in function_list:
        return choose
    elif fst.is_num(choose) and eval(choose) <= 4:
        return function_list[eval(choose) - 1]
    else:
        print("请您输入正确选项。")
        main()


# 初始化
def add_data(name):
    # 增加数据操作
    with sl.open("data") as data:
        time_str = time.strftime("   ————%Y/%m/%d %H:%M")
        if name not in data:
            while True:
                data_str = input("请输入您要留下的留言:") + time_str
                if input("是否确认添加(Y/N):") in yes_list:
                    break
                else:
                    main()
            data[name] = [data_str]
            # data.update({name: [data_str]})
            if input("是否继续添加留言(Y/N):") in yes_list:
                add_data(name)
            else:
                main()
        else:
            while True:
                data_str = input("请输入您要留下的留言:") + time_str
                if input("是否确认添加(Y/N):") in yes_list:
                    break
                else:
                    main()
            add_list = data[name]
            add_list.append(data_str)
            data[name] = add_list
            if input("是否继续添加留言(Y/N):") in yes_list:
                add_data(name)
            else:
                main()


def read_data(name):
    # 读取数据操作
    with sl.open("data") as data:
        if name == "adm":
            line = 1
            for i in data:
                print("[{0}]  {1:<10} :  {2}".format(line, i, data[i]))
                line = line + 1
            main()
        else:
            if name in data:
                print(data[name])
                main()
            else:
                print(f"没有给{name}的留言...")
                main()


def del_data(name):
    # 删除数据操作
    with sl.open("data") as data:
        if name == "adm":
            del_obj_name = input("请输入你要删除的留言人姓名:")
            if del_obj_name in data:
                print(f"正在删除 {del_obj_name} 的留言...")
                print(f"正在删除 {data[del_obj_name]}")
                print(f"{del_obj_name} 的留言已删除")
                del data[del_obj_name]
                if input("是否继续删除(Y/N):") in yes_list:
                    del_data(name)
                else:
                    main()
            else:
                print(f"没有给{del_obj_name}的留言...")
                fst.print_line(20, "——", True)
                main()

        elif name in data:
            del data[name]
            print(f"给{name}的留言 已删除...")
            main()
        else:
            print(f"没有给{name}的留言...")
            main()


def out():
    if input("是否确认退出(Y/N):") in yes_list:
        exit(0)
    else:
        main()


if __name__ == "__main__":
    main()
